package com.senlan.water.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senlan.water.common.exception.BusinessException;
import com.senlan.water.common.result.ResultCode;
import com.senlan.water.common.util.DateUtil;
import com.senlan.water.common.util.UserInfoUtil;
import com.senlan.water.dao.domain.MeetingRecords;
import com.senlan.water.dao.domain.Project;
import com.senlan.water.dao.domain.User;
import com.senlan.water.dao.service.MeetingRecordsService;
import com.senlan.water.dao.mapper.MeetingRecordsMapper;
import com.senlan.water.dto.project.MeetingRecordsAddDTO;
import com.senlan.water.dto.project.MeetingRecordsDTO;
import com.senlan.water.dto.project.MeetingRecordsEditDTO;
import com.senlan.water.dto.project.MeetingRecordsSearchDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
* @author 10499
* @description 针对表【meeting_records(会议记录)】的数据库操作Service实现
* @createDate 2024-12-11 16:43:39
*/
@Service
public class MeetingRecordsServiceImpl extends ServiceImpl<MeetingRecordsMapper, MeetingRecords>
    implements MeetingRecordsService{

    @Resource
    private UserInfoUtil userInfoUtil;

    @Override
    public Boolean addNew(MeetingRecordsAddDTO addDTO) {
        MeetingRecords meetingRecords = new MeetingRecords();
        BeanUtil.copyProperties(addDTO,meetingRecords);

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        meetingRecords.setCreateBy(userInfo.getNickname());
        meetingRecords.setUpdateBy(userInfo.getNickname());
        return baseMapper.insert(meetingRecords) > 0;
    }

    @Override
    public Boolean editById(MeetingRecordsEditDTO editDTO) {
        MeetingRecords meetingRecords = baseMapper.selectById(editDTO.getId());
        if (meetingRecords == null){
            throw new BusinessException(ResultCode.PROJECT_DATA_NOT_EXITS);
        }
        BeanUtil.copyProperties(editDTO,meetingRecords);

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        meetingRecords.setCreateBy(userInfo.getNickname());
        return baseMapper.updateById(meetingRecords) > 0;
    }

    @Override
    public Page<MeetingRecordsDTO> getPage(MeetingRecordsSearchDTO searchDTO) {
//        LambdaQueryWrapper<MeetingRecords> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(StrUtil.isNotBlank(searchDTO.getTitle()),MeetingRecords::getTitle,searchDTO.getTitle());
//        if (CollectionUtil.isNotEmpty(searchDTO.getCreateTime())){
//            queryWrapper.ge(MeetingRecords::getCreateTime,searchDTO.getCreateTime().get(0));
//            queryWrapper.le(MeetingRecords::getCreateTime, DateUtil.convertDate(searchDTO.getCreateTime().get(1)));
//        }
//
//        Page<MeetingRecords> page = new Page<>();
//        page.setCurrent(searchDTO.getPageNum());
//        page.setSize(searchDTO.getPageSize());
//        return baseMapper.selectPage(page,queryWrapper);

        if (CollectionUtil.isNotEmpty(searchDTO.getCreateTime())){
            searchDTO.getCreateTime().set(1,DateUtil.convertDate(searchDTO.getCreateTime().get(1)));
        }
        Page<MeetingRecords> page = new Page<>();
        page.setCurrent(searchDTO.getPageNum());
        page.setSize(searchDTO.getPageSize());
        return baseMapper.getPage(page,searchDTO);
    }

    @Override
    public Boolean deleteById(Long id) {
        MeetingRecords meetingRecords = baseMapper.selectById(id);
        if (meetingRecords == null){
            throw new BusinessException(ResultCode.PROJECT_DATA_NOT_EXITS);
        }

        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public List<MeetingRecords> getByProjectId(Long projectId) {
        LambdaQueryWrapper<MeetingRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MeetingRecords::getProjectId,projectId);
        return baseMapper.selectList(queryWrapper);
    }
}




